package com.xbang.attendance.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.xbang.attendance.service.AnalyzeService;

/**
 * @author huawei
 * @email alonelaval@gmail.com
 */
public abstract class AbstractAnalyzeService implements AnalyzeService {
	
	
	@Override
	public void analyse(File file, String type) throws Exception {
	}

	

	@Override
	public String analyse(File file, String path, Object... objs)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public void analyse(File file) throws Exception {
	}

	protected Map<String, Integer> excelFieldMapping = new HashMap<String, Integer>();

	/**
	 * @param cell
	 * @return
	 */
	public Object getCellValue(Cell cell) {
		try {
			if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
				// 字符串
				return cell.getStringCellValue();
			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
				return cell.getNumericCellValue();
			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
				return cell.getBooleanCellValue();
			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
				return "";
			} else
				return cell.getDateCellValue();
		} catch (Exception e) {
			return null;
		}
	}

	public Date getDateCellValue(Cell cell) {
		
		return cell == null ? null : cell.getDateCellValue();
	}
	

	public void setupExcelFieldMappings(HSSFSheet sheet) {
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				for (Iterator<Cell> cellIterator = row.cellIterator(); cellIterator.hasNext();) {
					Cell cell = cellIterator.next();
					Object object = getCellValue(cell);
					if (object != null) {
						excelFieldMapping.put(object.toString(), cell.getColumnIndex());
					}
				}
			} else
				break;
		}

	}
	/**
	 * get Property Name
	 * @param index
	 * @param map
	 * @return
	 */
	protected String getPropertyName(int index,Map<String,String> map) {
		for (Map.Entry<String, Integer> entry : excelFieldMapping.entrySet()) {
			if(entry.getValue().equals(index)){
				return map.get(entry.getKey());
			}
		}
		return null;
	}

	
	/**
	 * @return the excelFieldMapping
	 */
	public Map<String, Integer> getExcelFieldMapping() {
		return excelFieldMapping;
	}

	/**
	 * @param excelFieldMapping
	 *            the excelFieldMapping to set
	 */
	public void setExcelFieldMapping(Map<String, Integer> excelFieldMapping) {
		this.excelFieldMapping = excelFieldMapping;
	}

}
